package slr.comment.portlet.ui;

import java.util.List;

import slr.comment.model.SlrAnchor;
import slr.comment.model.SlrComment;
import slr.comment.portlet.data.SlrExplorerContainer;
import slr.comment.portlet.util.Lang;
import slr.comment.portlet.util.SLRConstants;

import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.util.Validator;
import com.vaadin.data.util.BeanItemContainer;
import com.vaadin.event.DataBoundTransferable;
import com.vaadin.event.dd.DragAndDropEvent;
import com.vaadin.event.dd.DropHandler;
import com.vaadin.event.dd.acceptcriteria.AcceptAll;
import com.vaadin.event.dd.acceptcriteria.AcceptCriterion;
import com.vaadin.terminal.gwt.client.ui.dd.VerticalDropLocation;
import com.vaadin.ui.AbstractSelect;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Component;
import com.vaadin.ui.Label;
import com.vaadin.ui.Table;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;

/**
 * List added to the SLR comments.
 * 
 * @author TransIT mPower Labs
 * 
 */
@SuppressWarnings("serial")
public class SLRCommentsTable extends Table {
	private static final long serialVersionUID = -5055287277846799104L;
	public final Table anchorTable = new Table(Lang.getKey("slr.comment.comments-list"));

	public Component addCommentListComponent(Object object, List<SlrComment> slrCommentList, final VerticalLayout second) {

		if (object instanceof SlrAnchor) {
			SlrExplorerContainer.listName = Lang.getKey("slr.comment.list-of-comments");
			BeanItemContainer<SlrComment> slrDocContainer = null;

			final SlrAnchor slrAnchor = (SlrAnchor) object;

			try {
				if (Validator.isNull(slrCommentList) || slrCommentList.size() <= 0) {
					return new Label(Lang.getKey("slr.comment.no-comments-available"));
				}
			} catch (Exception e) {
				_log.error(e.getMessage());
			}
			slrDocContainer = new BeanItemContainer<SlrComment>(slrCommentList);

			anchorTable.setImmediate(true);
			anchorTable.setHeight(100, UNITS_PERCENTAGE);
			anchorTable.setWidth(100, UNITS_PERCENTAGE);

			anchorTable.setContainerDataSource(slrDocContainer);
			anchorTable.setVisibleColumns(SLRConstants.SLRCOMMENT.split(","));

			anchorTable.addGeneratedColumn("slrCommentText", new ColumnGenerator() {
				public Component generateCell(Table source, Object itemId, Object columnId) {
					String slrCommentText = (String) source.getContainerProperty(itemId, "slrCommentText").getValue();
					VerticalLayout htmlLayout = new VerticalLayout();
					htmlLayout.addComponent(new Label(slrCommentText, Label.CONTENT_XHTML));

					return htmlLayout;
				}
			});

			anchorTable.addGeneratedColumn("editComment", new ColumnGenerator() {

				public Component generateCell(Table source, final Object itemId, Object columnId) {
					VerticalLayout htmlLayout = new VerticalLayout();
					Button editComment = new Button(Lang.getKey("slr.comment.edit-button"), new Button.ClickListener() {
						public void buttonClick(ClickEvent event) {
							getDialouge(anchorTable, (SlrComment) itemId, slrAnchor, second);
						}
					});

					htmlLayout.addComponent(editComment);

					return htmlLayout;
				}
			});

			return anchorTable;

		}
		return new Label(Lang.getKey("slr.comment.no-list-available"));
	}

	/**
	 * Displays the window containig the SLR Comment fields
	 * 
	 * @param anchorTable
	 * @param selectedItem
	 * @param slrAnchor
	 * @param second
	 */
	protected void getDialouge(Table anchorTable, final SlrComment selectedItem, final SlrAnchor slrAnchor,
			VerticalLayout second) {
		final Window subWin = new Window(Lang.getKey("slr.comment.slr-comments"));
		final SLRCommentForm commentsForm = new SLRCommentForm(slrAnchor, selectedItem.getSlrDocumentId(), second,
				subWin, selectedItem, selectedItem.getSlrCommentTyp());
		subWin.setModal(true);
		subWin.center();
		subWin.setWidth(60, UNITS_PERCENTAGE);
		subWin.addComponent(commentsForm);

		anchorTable.getApplication().getMainWindow().addWindow(subWin);
	}

	/**
	 * The drag and drop of the SLR Comment events are handled here.
	 * @param order
	 */
	public void setRowDraggable(String order) {
		if (Validator.isNotNull(order) && order.equalsIgnoreCase(Lang.getKey("slr.comment.reorder"))) {
			anchorTable.setDragMode(TableDragMode.ROW);
			anchorTable.setDropHandler(new DropHandler() {

				public AcceptCriterion getAcceptCriterion() {
					return AcceptAll.get();
				}

				public void drop(DragAndDropEvent event) {
					DataBoundTransferable transferable = (DataBoundTransferable) event.getTransferable();
					Object sourceRow = transferable.getItemId();
					AbstractSelect.AbstractSelectTargetDetails dropData = ((AbstractSelect.AbstractSelectTargetDetails) event
							.getTargetDetails());
					Object targetRow = dropData.getItemIdOver();
					if ((sourceRow == targetRow) || (targetRow == null)) {
						return;
					}
					anchorTable.removeItem(sourceRow);
					if (dropData.getDropLocation() == VerticalDropLocation.BOTTOM) {
						anchorTable.addItemAfter(targetRow, sourceRow);
					} else {
						Object rowAbove = anchorTable.prevItemId(targetRow);
						anchorTable.addItemAfter(rowAbove, sourceRow);
					}
				}
			});

		} else if (Validator.isNotNull(order) && order.equalsIgnoreCase(Lang.getKey("slr.comment.quit-reordering"))) {
			anchorTable.setDragMode(TableDragMode.NONE);
		}

	}
    private static Log _log = LogFactoryUtil.getLog(SLRCommentsTable.class);

}